home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7518 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: newsroom.hitc.com!usenet
  2. From: psand@eos.hitc.com (G. Patrick Sand)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: what happens w/delete called twice ?
  5. Date: 23 Feb 1996 17:48:27 GMT
  6. Organization: Hughes Aircraft (EOSDIS)
  7. Message-ID: <4gkulb$gkr@newsroom.hitc.com>
  8. References: <kcc.423.0EE12CF6@interaccess.com>
  9. NNTP-Posting-Host: 155.157.118.56
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.99.3
  12.  
  13. In article <kcc.423.0EE12CF6@interaccess.com>, kcc@interaccess.com 
  14. says...
  15. >
  16. >Hello,
  17. >
  18. >        Can someone tell me... what happens when delete is called twice 
  19. for the
  20. > same 
  21. >memory deallocation [SNIP!]
  22.  
  23. You have probably caused yourself a lot of pain...and may be creating a 
  24. memory allocation error to boot...
  25.  
  26. Unless you (or your compiler--very unlikely) sets to pointer to zero 
  27. after
  28. deallocating with delete, you could have the following situations occur:
  29.  
  30. 1. in a multi-threaded environment, you may have just "freed" up some 
  31. memory
  32. which somebody else has allocated, making it likely that memory 
  33. corruption will occur soon.
  34.  
  35. 2. in a multi-user environment, a similar thing may happen between 
  36. distinct processes.
  37.  
  38. 3. if you compiler's run-time library (or a 3rd-party memory manager) is 
  39. clever enough to detect when you attempt to deallocate memory at a 
  40. nonzero address that is already deallocated, you will get a run-time 
  41. exception--not a nice thing to have happen...  If they don't do it, you 
  42. will have lots of fun diagnosing and debugging this one...
  43.  
  44. As I understand things (subject to brainlocks now and then...), you can 
  45. always delete a pointer that has address 0 as its' value--this C++ 
  46. loophole is to allow you to gracefully handling deleting "an object which 
  47. wasn't fully constructed by the constructor"--which should return a zero 
  48. pointer value...
  49.  
  50. So, I guess you either force the pointer to 0 after deleting the first 
  51. time, or (better yet) only delete the object once.  Check out Effective 
  52. c++ by Scott Meyers (ISBN 0-201-56364-9) Items 6 and 7; you might also 
  53. want to check out his new book (I haven't gotten it yet).  Look at 
  54. http://www.aw.com
  55. for the Addison-Wesley home page and cruise on down the the 
  56. Addison-Wesley Professional Computing Series pages...
  57.  
  58.  
  59. -- 
  60. G. Patrick Sand
  61. psand@eos.hitc.com
  62. PatSand@aol.com
  63. (301) 925-0791
  64. "Travel Light But Right..."
  65. Microsoft Network is prohibited from redistributing 
  66. this work in any form, in whole or in part.   License 
  67. to distribute this individual post is available to Microsoft
  68. for $999. Posting without permission constitutes an 
  69. agreement to these terms...gps
  70.  
  71.